package com.example.e_book.Activity;

import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;

import com.example.e_book.Adapter.ViewPagerAdapter;
import com.example.e_book.DAO.BookshelfDAO;
import com.example.e_book.Fragment.ClassifyFragment;
import com.example.e_book.Fragment.HomeFragment;
import com.example.e_book.Fragment.MyFragment;
import com.example.e_book.R;
import com.example.e_book.entity.User;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

public class MainActivity extends AppCompatActivity {
    private ViewPager2 viewPager2;
    private TabLayout tabLayout;
    private BookshelfDAO bookshelfDAO;
    private User user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 获取传递过来的用户信息
        Intent intent = getIntent();
        user = intent.getParcelableExtra("USER");
        bookshelfDAO = new BookshelfDAO(this, user);

        viewPager2 = findViewById(R.id.viewPager2);
        setupViewPager(viewPager2);

        // 如果使用TabLayout作为底部导航
        tabLayout = findViewById(R.id.tabs);
        new TabLayoutMediator(tabLayout, viewPager2,
                (tab, position) -> tab.setText(TAB_TITLES[position])
        ).attach();
    }

    private void setupViewPager(ViewPager2 viewPager2) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(this);
        Bundle bundle = new Bundle();
        bundle.putParcelable("USER", user);
        adapter.addFragment(new HomeFragment(), "主页", bundle);
        adapter.addFragment(new ClassifyFragment(), "分类", bundle);
        adapter.addFragment(new MyFragment(), "我的", bundle);
        viewPager2.setAdapter(adapter);
    }

    private static final String[] TAB_TITLES = new String[]{"主页", "分类", "我的"};
}